import type { IPaginationPlatformConfig } from '../../../../module/page_enhancer/interaction/auto_pagination/service/config/types'

/**
 * 页面类型
 */
export enum GithubPageType {
  Index = 'index',
  Project = 'project',
  CodeView = 'code-view',
  Commits = 'commits',
  Tags = 'tags',
  NotFound = 'not-found',
  Undefined = 'undefined',
}

/**
 * 页面类型属性
 */
export interface GithubTypeResult {
  type: GithubPageType | 'default'
}

// ====================================================== 基础配置 ======================================================
// 分页组件
const DEFAULT_PAGINATION_CONTAINER_SELECTOR: Partial<Record<GithubPageType, string>> = {
  [GithubPageType.Commits]: 'h2:contains("Pagination") + div > div > div',
  [GithubPageType.Tags]: '.paginate-container .pagination',
}

export const DEFAULT_LIST_CONTAINER_SELECTOR: Partial<Record<GithubPageType, string>> = {
  [GithubPageType.Commits]: 'h2 + div[class*="Timeline-Timeline"]',
  [GithubPageType.Tags]: '.repository-content  .Box .Box-body',
}

export const GITHUB_PAGINATION_CONFIG: IPaginationPlatformConfig = {
  className: {
    previousPage: '',
    nextPage: '',
    currentPage: '',
    allPage: '',
  },
  childSelector: {
    previousPage: 'a:first-child',
    nextPage: 'a:last-child',
    currentPage: '',
    allPage: 'a',
  },
  selector: {
    container: DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Tags] || '',
    previousPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Tags] || ''} a:first-child`,
    nextPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Tags] || ''} a:last-child`,
    currentPage: ``,
    item: ``,
    link: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Tags] || ''} a`,
  },
  list: {
    container: DEFAULT_LIST_CONTAINER_SELECTOR[GithubPageType.Tags] || '',
    item: `${DEFAULT_LIST_CONTAINER_SELECTOR[GithubPageType.Tags] || ''} > .Box-row`,
  },
}

export const GITHUB_PAGE_PAGINATION_CONFIG: Partial<Record<GithubPageType, IPaginationPlatformConfig>> = {
  [GithubPageType.Commits]: {
    ...GITHUB_PAGINATION_CONFIG,
    selector: {
      ...GITHUB_PAGINATION_CONFIG.selector,
      container: DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Commits] || '',
      previousPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Commits] || ''} a[data-testid="pagination-prev-button"]`,
      nextPage: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Commits] || ''} a[data-testid="pagination-next-button"]`,
      link: `${DEFAULT_PAGINATION_CONTAINER_SELECTOR[GithubPageType.Commits] || ''} a[type="button"]`,
    },
    list: {
      ...GITHUB_PAGINATION_CONFIG.list,
      container: DEFAULT_LIST_CONTAINER_SELECTOR[GithubPageType.Commits] || '',
      item: `${DEFAULT_LIST_CONTAINER_SELECTOR[GithubPageType.Commits] || ''} > .Timeline-Item`,
    },
  },
  [GithubPageType.Tags]: GITHUB_PAGINATION_CONFIG,
}
